﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

//[assembly: WebResource("StarRatingControl.css.base.css", "text/css", PerformSubstitution = true)]
//[assembly: WebResource("StarRatingControl.css.jquery-ui-1.7.2.custom.css", "text/css", PerformSubstitution = true)]
//[assembly: WebResource("StarRatingControl.css.ui.stars.css", "text/css", PerformSubstitution = true)]
//[assembly: WebResource("StarRatingControl.css.ui.stars.min.css", "text/css", PerformSubstitution = true)]
//[assembly: WebResource("StarRatingControl.images.ui.stars.gif", "img/gif")]
//[assembly: WebResource("StarRatingControl.images.crystal-stars.png", "img/png")]
//[assembly: WebResource("StarRatingControl.js.1.js", "text/javascript")]
//[assembly: WebResource("StarRatingControl.js.2.js", "text/javascript")]
//[assembly: WebResource("StarRatingControl.js.3.js", "text/javascript")]
//[assembly: WebResource("Nop.Controls.StarRating.Scripts.stars.js", "text/javascript")]

namespace NopSolutions.NopCommerce.Controls
{
    /// <summary>
    /// Controle para realizar avaliação em websites ASP.NET
    /// </summary>
    [DefaultProperty("JSFunction")]
    [ParseChildren(true)]
    [PersistChildren(true)]
    [ToolboxData("<{0}:StarRating runat=server />")]
    public class StarRating : WebControl
    {
        [Bindable(true)]
        [Localizable(true)]
        public string JSFunction {
            get {
                return Convert.ToString(ViewState["JSFunction"]);
            }

            set {
                ViewState["JSFunction"] = value;
            }
        }

        [Bindable(true)]
        [Localizable(true)]
        public bool ShowCaption {
            get {
                if (ViewState["ShowCaption"] == null) {
                    return true;
                }
                else {
                    return Convert.ToBoolean(ViewState["ShowCaption"]);
                }
            }

            set {
                ViewState["ShowCaption"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Bindable(true)]
        [Localizable(true)]
        public List<StarItem> Items { get; set; }

        private void LoadStyleSheets() {
            //Assembly ass = Assembly.GetExecutingAssembly();
            //List<string> resources = new List<string>(ass.GetManifestResourceNames().Where(p => p.EndsWith(".css")));

            //foreach (string item in resources) {
            //    HtmlLink link = new HtmlLink();
            //    link.Attributes.Add("type", "text/css");
            //    link.Attributes.Add("rel", "stylesheet");
            //    link.Attributes.Add("href", this.Page.ClientScript.GetWebResourceUrl(typeof(StarRating), item));
            //    this.Page.Header.Controls.Add(link);
            //}
        }

        private void LoadJavascript() {
            Assembly ass = Assembly.GetExecutingAssembly();
            List<string> resourcesJs = new List<string>(ass.GetManifestResourceNames().Where(p => p.EndsWith(".js")).OrderBy(p => p));

            foreach (string item in resourcesJs) {
                this.Page.ClientScript.RegisterClientScriptInclude(item, this.Page.ClientScript.GetWebResourceUrl(typeof(StarRating), item));
            }
        }

        //protected override void OnPreRender(EventArgs e) {
        //    this.LoadJavascript();
        //    this.LoadStyleSheets();
        //    base.OnPreRender(e);
        //}

        protected override void RenderContents(HtmlTextWriter output) {
            //StringBuilder html = new StringBuilder();

            if (ShowCaption) {
                output.AddAttribute(HtmlTextWriterAttribute.Class, "capsTitle");
                output.RenderBeginTag(HtmlTextWriterTag.Div);

                output.Write("Nota: ");

                output.AddAttribute(HtmlTextWriterAttribute.Id, "stars_cap");
                output.AddAttribute(HtmlTextWriterAttribute.Class, "caps");
                output.RenderBeginTag(HtmlTextWriterTag.Div);
                output.RenderEndTag();

                output.RenderEndTag();

                //html.AppendLine("<span class=\"capsTitle\">Nivel: <span id=\"stars_cap\" class=\"caps\"></span></span>");
            }

            output.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("start_wrapper_{0}", ID));
            output.RenderBeginTag(HtmlTextWriterTag.Div);

            foreach (var item in Items) {
                output.AddAttribute(HtmlTextWriterAttribute.Value, item.Value.ToString());
                output.AddAttribute(HtmlTextWriterAttribute.Title, item.Title);
                if (item.Checked) {
                    output.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
                }
                output.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
                output.RenderBeginTag(HtmlTextWriterTag.Input);
                output.RenderEndTag();
            }

            output.RenderEndTag();

            //html.AppendLine(string.Format("<div id=\"stars_wrapper_{0}\">", this.ID));

            //foreach (StarItem item in this.Items) {
            //    html.Append(new HtmlInputRadio() { Value = item.Value.ToString(), Title = item.Title, Checked = item.Checked });
            //}

            //html.AppendLine("</div>");
            //output.Write(html.ToString());

            string script = @"
                var $stars1 = $('#@div'); 
                $stars1.stars({ 
                    inputType: 'radio', 
                    captionEl: $('#stars_cap'), 
                    callback: function(ui, type, value) { 
                        eval('@fn(' + value + ');'); 
                    }, 
                    cancelClass: 'ui-crystal-cancel', 
                    starClass: 'ui-crystal-star', 
                    starOnClass: 'ui-crystal-star-on', 
                    starHoverClass: 'ui-crystal-star-hover', 
                    cancelHoverClass: 'ui-crystal-cancel-hover', 
                    cancelShow: false 
                });";

            //switch (this.StarType) {
            //    case StarType.Default:
            //        script = Resources.JSDefault;
            //        break;
            //    case StarType.Crystal:
            //        script = Resources.JSCrystal;
            //        break;
            //}

            script = script.Replace("@div", this.ClientID);
            script = script.Replace("@fn", this.JSFunction);
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), script, true);
        }
    }
}
